package com.itheima.easy.adapter.impl;

import com.itheima.easy.adapter.AppPayAdapter;
import com.itheima.easy.constant.TradingConstant;
import com.itheima.easy.handler.AppPayHandler;
import com.itheima.easy.utils.RegisterBeanHandler;
import com.itheima.easy.vo.TradingVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName AppPayAdapter.java
 * @Description 原生APP支付适配支付适配实现
 */
@Slf4j
@Component
public class AppPayAdapterImpl implements AppPayAdapter {

    @Autowired
    RegisterBeanHandler registerBeanHandler;

    private static Map<String,String> appPayHandlers =new HashMap<>();

    static {
        appPayHandlers.put(TradingConstant.TRADING_CHANNEL_ALI_PAY,"aliAppPayHandler");
        appPayHandlers.put(TradingConstant.TRADING_CHANNEL_WECHAT_PAY,"wechatAppPayHandler");
    }

    /***
     * @description 外部商户APP唤起快捷SDK创建订单并支付
     * @param tradingVo 订单
     * @return  TradingVo对象中的placeOrderJson：会话秘钥
     */
    public TradingVo appTrading(TradingVo tradingVo) {
        //根据渠道获得bean名称
        String appPayHandlerString = appPayHandlers.get(tradingVo.getTradingChannel());

        //从IOC容器中找到AppPayHandler实现
        AppPayHandler appPayHandler = registerBeanHandler.getBean(appPayHandlerString, AppPayHandler.class);

        //appTrading支付交易处理
        return appPayHandler.appTrading(tradingVo);
    }
}
